<template>
  <el-dialog
    title="编辑"
    :visible.sync="visible"
    width="75vw"
    top="8vh"
    :close-on-click-modal="false"
  >
    <div class="boxsty">
      <el-row>
        <el-form :model="dataForm" ref="dataForm" label-width="130px">
          <el-col :span="24">
            <el-col :span="6">
              <el-form-item label="任务书名称:" prop="workAssignName">
                <el-input
                  disabled
                  v-model="dataForm.workAssignName"
                  placeholder="任务书名称"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="任务书有效期:" required>
                <el-col :span="10">
                  <el-form-item prop="workAssignYxqq">
                    <el-date-picker
                      disabled
                      v-model="dataForm.workAssignYxqq"
                      type="date"
                      value-format="yyyy-MM-dd"
                      placeholder="开始日期"
                      style="width: 100%"
                    ></el-date-picker>
                  </el-form-item>
                </el-col>
                <el-col :span="4">
                  <div class="text-center">至</div>
                </el-col>
                <el-col :span="10">
                  <el-form-item prop="workAssignYxqz">
                    <el-date-picker
                      disabled
                      v-model="dataForm.workAssignYxqz"
                      type="date"
                      value-format="yyyy-MM-dd"
                      placeholder="结束日期"
                      style="width: 100%"
                    ></el-date-picker>
                  </el-form-item>
                </el-col>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="模板名称:" prop="templateId">
                <el-input
                  placeholder="请输入内容"
                  v-model="dataForm.templateName"
                  :disabled="true">
                </el-input>
              </el-form-item>
            </el-col>
          </el-col>
        </el-form>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-col :span="12">
            <div>
              <div
                style="
                  float: left;
                  width: 118px;
                  margin-right: 12px;
                  text-align: right;
                "
              >
                <el-button type="text" icon="el-icon-user-solid"></el-button>
              </div>
              <div style="float: left">
                <div>被考核人</div>
                <div>
                  <span class="mg-r15">
                    {{ dataForm.ryName }}
                  </span>
                </div>
              </div>
            </div>
          </el-col>
        </el-col>
      </el-row>
    </div>
    <div style="margin-top: 20px;display: flex;justify-content: space-between;">
      <div>
        <el-button
          plain
          type="primary"
          @click="showZhibiaoTable"
          v-if="types === 'qtjc'"
          >指标库</el-button
        >
        <el-button type="primary" @click="cheekBtn" v-if="types == 'qtjc'"
          >查看领导审批意见</el-button
        >
      </div>
      <div>
        <el-button
          @click="saveBtntp"
          type="primary"
          v-if="showType == 15 || showType == 16"
          >保存</el-button
        >
        <el-button
          @click="saveBtnPjdf"
          type="primary"
          v-if="showType == 17"
          >保存</el-button
        >
        <el-button @click="saveBtnzp" type="primary" v-if="showType == 14"
          >保存</el-button
        >
        <el-button @click="saveBtnqtjc" type="primary" v-if="showType == 19"
          >保存</el-button
        >
        <el-button @click="closeBtn">关闭</el-button>
      </div>
    </div>
    <el-table
      :data="dataList"
      border
      style="width: 100%; margin-top: 20px"
      min-height="60vh"
      ref="table"
      v-loading="loading"
    >
      <el-table-column
        prop="zhibiaoType"
        label="指标类别"
        min-width="90"
        header-align="center"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="zhibiaoName"
        label="指标名称"
        min-width="120"
        show-overflow-tooltip
        header-align="center"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="weight"
        label="权重(%)"
        min-width="110"
        header-align="center"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="formulaId"
        label="计算公式"
        min-width="180"
        header-align="center"
        align="center"
      >
        <template slot-scope="scope">
          <span>
            <span v-for="item in formulaOptions">
              <span v-if="scope.row.formulaId == item.id">{{ item.gsMc }}</span>
            </span>
          </span>
        </template>
      </el-table-column>
      <el-table-column
        prop="targetValue"
        label="目标值"
        min-width="200"
        header-align="center"
        align="center"
      >
        <template slot-scope="scope">
          <el-input
            disabled
            type="textarea"
            :rows="3"
            style="white-space: pre-wrap"
            v-model="scope.row.targetValue"
            placeholder="请输入内容"
          />
        </template>
      </el-table-column>
      <el-table-column
        prop="unit"
        label="单位"
        min-width="100"
        header-align="center"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="targetValueType"
        label="完成值获取方式"
        min-width="180"
        header-align="center"
        align="center"
      >
        <template slot-scope="scope">
          <span v-for="item in typeList">
            <span v-if="scope.row.targetValueType == item.value">{{
              item.name
            }}</span>
          </span>
        </template>
      </el-table-column>
      <el-table-column
        prop="khrName"
        label="考核人"
        min-width="180"
        header-align="center"
        align="center"
      >
      </el-table-column>
      <el-table-column
        v-if="showType == 14||showType == 15||showType == 16"
        prop="tbSz"
        label="填报值"
        min-width="120"
        header-align="center"
        align="center"
      >
        <template slot-scope="scope">
          <el-input
            v-model="scope.row.tbSz"
            placeholder="请输入内容"
            @change="saveProperty(scope.row, scope.$index)"
          />
        </template>
      </el-table-column>
      <!-- 自评显示 showType == 15-->
      <el-table-column
        v-if="showType == 14"
        prop="zpDf"
        label="自评得分"
        min-width="180"
        header-align="center"
        align="center"
      >
        <template slot-scope="scope">
          <el-input-number
            v-model="scope.row.zpDf"
            placeholder="请输入内容"
            @change="saveProperty(scope.row, scope.$index)"
          ></el-input-number>
        </template>
      </el-table-column>
      <!-- 他评显示 showType == 15  和他评完成 16-->
      <el-table-column
        v-if="showType == 15 || showType == 16 || showType == 17"
        prop="tpDf"
        label="评价得分"
        min-width="180"
        header-align="center"
        align="center"
      >
        <template slot-scope="scope">
          <el-input-number
            v-model="scope.row.tpDf"
            placeholder="请输入内容"
          ></el-input-number>
        </template>
      </el-table-column>
      <!-- 结果汇总显示 showType == 17 -->
      <el-table-column
        v-if="showType == 17 || showType == 19 || showType == 18"
        prop="zpDf"
        label="自评得分"
        min-width="180"
        header-align="center"
        align="center"
      >
      </el-table-column>
      <el-table-column
        v-if="showType == 19 || showType == 18"
        prop="tpDf"
        label="评价得分"
        min-width="180"
        header-align="center"
        align="center"
      >
      </el-table-column>
      <el-table-column
        v-if="showType == 19"
        prop="qtjcDf"
        label="其他奖惩"
        min-width="180"
        header-align="center"
        align="center"
      >
        <template slot-scope="scope">
          <el-input-number
            v-model="scope.row.qtjcDf"
            placeholder="请输入内容"
          ></el-input-number>
        </template>
      </el-table-column>
      <el-table-column
        v-if="showType == 19"
        prop="qtjcReason"
        label="其他奖惩原因"
        min-width="180"
        header-align="center"
        align="center"
      >
        <template slot-scope="scope">
          <el-input
            v-model="scope.row.qtjcReason"
            placeholder="请输入其他奖惩原因"
          ></el-input>
        </template>
      </el-table-column>
      <el-table-column
        v-if="showType == 18"
        prop="qtjcDf"
        label="其他奖惩"
        min-width="180"
        header-align="center"
        align="center"
      >
      </el-table-column>
      <el-table-column
        v-if="showType == 18"
        prop="qtjcReason"
        label="其他奖惩原因"
        min-width="180"
        header-align="center"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="isziping"
        label="是否自评"
        min-width="120"
        show-overflow-tooltip
        header-align="center"
        align="center"
      >
        <template slot-scope="scope">
          <el-switch
            disabled
            v-model="scope.row.isziping"
            active-color="#13ce66"
            inactive-color="#CCCCCC"
            active-value="true"
            inactive-value="false"
            active-text="是"
            inactive-text="否"
          >
          </el-switch>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      title="任务书指标库"
      :visible.sync="dialogVisible"
      width="80%"
      append-to-body
    >
      <zhibiaoTable ref="zhibiaoRef" @func="getRow"></zhibiaoTable>
      <span slot="footer" class="dialog-footer">
        <el-button plain type="primary" @click="addZhibiao">添 加</el-button>
        <el-button plain @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="领导审批意见"
      :visible.sync="yjshow"
      width="40%"
      append-to-body
    >
      <el-table :data="yjtableData" style="width: 100%">
        <el-table-column
          prop="ry_name"
          label="审批人"
          width="180"
          header-align="center"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="message_"
          label="审批意见"
          width="180"
          header-align="center"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="time_"
          label="时间"
          header-align="center"
          align="center"
        >
        </el-table-column>
      </el-table>
    </el-dialog>
  </el-dialog>
</template>

<script>
import {
  getListByzbnd,
  getListByzbndfortp,
  getListBytp,
  getListByzp
} from "@/api/hetongrenwu/book/bookcj";
import { addHetongHtzb } from "@/api/hetongrenwu/hetongyeji/hetongzb";
import {
  saveBatchForTp,
  saveBatchForPjdf,
  saveBatchForZp,
  saveBatchForQtjc,
  calcuScore,
  getnormlist,
  getLdshOpinions,
} from "@/api/hetongrenwu/book/bookFq";
import {
  getFormula,
  getDataProvider,
} from "@/api/hetongrenwu/hetongyeji/hetongzb";
import zhibiaoTable from "@/views/hetongrenwu/hetong/hetongyeji/zhibiaoTable";
export default {
  components: {
    zhibiaoTable,
  },
  data() {
    return {
      visible: false,
      dialogVisible: false,
      dataForm: {},
      formulaOptions: [],
      typeList: [
        { name: "手工填报", value: "0" },
        { name: "自动获取", value: "1" },
      ],
      dataList: [],
      providerList: [],
      newRows: [],
      fayuerendataList: [],
      disabled: true,
      fullscreenLoading: false,
      fullscreenLoadingadd: false,
      loading: false,
      sortingPerChooseDialogData: {},
      showType: "",
      types: "",
      dialogVisible: false,
      yjshow: false,
      yjtableData: [],
    };
  },
  watch: {},
  created() {},
  methods: {
    init(row, types) {
      this.types = types;
      if (types == "qtjc") {
        getLdshOpinions({ cjId: row.id }).then((res) => {
          this.yjtableData = res.data.data;
        });
      }
      this.showType = row.state;
      this.getXiaLa();
      this.visible = true;
      this.dataForm = row;
      this.grtList();
    },
    getXiaLa() {
      getFormula().then((res) => {
        this.formulaOptions = res.data.data;
      });
      getDataProvider().then((res) => {
        if (res.data.data) {
          this.providerList = res.data.data;
        }
      });
    },
    grtList() {
      this.loading = true;
      // 自评
      if (this.showType == 14) {
        getListByzp({ cjId: this.dataForm.id, isziping: "true" }).then(
          (response) => {
            this.dataList = response.data.data;
            this.resetDataList();
            this.loading = false;
          }
        );
      } else if (this.showType == 15||this.showType == 16) {
        // 他评
        getListBytp({ cjId: this.dataForm.id }).then((response) => {
          if (this.showType == 15) {
            response.data.data.forEach((ele) => {
              if (ele.tpDf == "") {
                // 当是他评时，把自评的分值赋给他评
                ele.tpDf = ele.zpDf;
              }
            });
          }
          this.dataList = response.data.data;
          this.resetDataList();
          this.loading = false;
        });
      } else {
        // 其他页
        getListByzbnd({ cjId: this.dataForm.id }).then((response) => {
          this.dataList = response.data.data;
          this.resetDataList();
          this.loading = false;
        });
      }
    },
    closeBtn() {
      this.visible = false;
      this.dataForm = {};
      this.dataList = [];
      this.$emit("fqcommonchild", "");
    },
    // 他评
    saveBtntp() {
      var data = this.dataList;
      // for (var i = 0; i < data.length; i++) {
      //   if (data[i].tpDf == "") {
      //     this.$message.warning("请填写" + [i + 1] + "行评价值");
      //     return;
      //   }
      // }
      saveBatchForTp(data).then((res) => {
        this.$message.closeAll();
        this.$message.success("保存成功");
        this.closeBtn();
      });
    },
    // 保存评价得分
    saveBtnPjdf() {
      var data = this.dataList;
      // for (var i = 0; i < data.length; i++) {
      //   if (data[i].tpDf == "") {
      //     this.$message.warning("请填写" + [i + 1] + "行评价值");
      //     return;
      //   }
      // }
      saveBatchForPjdf(data).then((res) => {
        this.$message.closeAll();
        this.$message.success("保存成功");
        this.closeBtn();
      });
    },
    // 保存评价得分
    saveBtnPjdf() {
      var data = this.dataList;
      // for (var i = 0; i < data.length; i++) {
      //   if (data[i].tpDf == "") {
      //     this.$message.warning("请填写" + [i + 1] + "行评价值");
      //     return;
      //   }
      // }
      saveBatchForPjdf(data).then((res) => {
        this.$message.closeAll();
        this.$message.success("保存成功");
        this.closeBtn();
      });
    },
    // 其他奖惩
    saveBtnqtjc() {
      var data = this.dataList;
     /* for (var i = 0; i < data.length; i++) {
        console.log(data[i]);
        if (data[i].qtjcReason == null) {
          this.$message.warning("请填写" + [i + 1] + "行其他奖惩原因");
          return;
        }
        if (data[i].qtjcDf == undefined) {
          this.$message.warning("请填写" + [i + 1] + "行其他奖惩分值");
          return;
        }
      }*/
      saveBatchForQtjc(data).then((res) => {
        this.$message.closeAll();
        this.$message.success("保存成功");
        this.closeBtn();
      });
    },
    // 自评
    saveBtnzp() {
      var data = this.dataList;
      saveBatchForZp(data).then((res) => {
        this.$message.closeAll();
        this.$message.success("保存成功");
        this.closeBtn();
      });
    },
    cheekBtn() {
      this.yjshow = true;
    },
    //重组dataList
    resetDataList() {
      this.dataList = this.dataList.map((item) => {
        item.providerList = [];
        item.tgLx = (item.tgLx ? item.tgLx : 1) || "2";
        //tgLx等于1是机构，2是个人
        if (item.tgLx == "2") {
          item.dataProviderIdSelect = item.targetUserId;
          item.dataProviderNameSelect = item.targetUserName;
        } else {
          item.dataProviderIdSelect = item.dataProviderId;
          item.dataProviderNameSelect = item.dataProvider;
        }
        if (item.targetUserId) {
          item.providerList.push({
            val: item.targetUserId,
            label: item.targetUserName,
          });
        }
        if (item.dataProviderId) {
          item.providerList.push({
            val: item.dataProviderId,
            label: item.dataProvider,
          });
        }
        if (item.targetValue) {
          item.targetValue = item.targetValue.replace(/(；|;)/g, ";\n");
        }
        return item;
      });
    },
    showZhibiaoTable() {
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs.zhibiaoRef.init("tbsh", 3);
        this.$refs.zhibiaoRef.getDataList();
      });
    },
    addZhibiao() {
      this.$refs.zhibiaoRef.saveZhibiao(this.dataList);
    },
    getRow(rows) {
      addHetongHtzb({
        rows: rows,
        hetongId: 0,
      }).then((res) => {
        var arr = res.data.data;
        arr.forEach((ele) => {
          ele.cjId = this.dataForm.id;
        });
        this.dataList = this.dataList.concat(arr);
      });
      this.dialogVisible = false;
    },
    // 保存得分
    saveProperty(row, index) {
      if (row.zhibiaoType == "奖惩类") {
        getnormlist({ normjks: row.zbDf, normid: row.zhibiaoId }).then(
          (res) => {
            if (res.data.data == 0) {
              this.$confirm(
                "您填写的得分已超出加扣范围，是否继续填写得分！",
                "提示",
                {
                  confirmButtonText: "确定",
                  cancelButtonText: "取消",
                  type: "warning",
                }
              )
                .then((res) => {
                  calcuScore(row)
                    .then((res) => {
                      this.$message.closeAll();
                      this.$message.success("保存成功！");
                    })
                    .then((res) => {
                      this.grtList();
                    });
                })
                .catch((res) => {
                  this.grtList();
                });
            } else {
              calcuScore(row).then((res) => {
                this.$message.closeAll();
                this.$message.success("保存成功！");
                //刷新列表
                this.grtList();
              });
            }
          }
        );
      } else {
        calcuScore(row).then((res) => {
          this.$message.closeAll();
          this.$message.success("保存成功！");
          //刷新列表
          this.grtList();
        });
      }
    },
  },
};
</script>
